﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        #region fonctions managers
        ////////////////////////FONCTIONS MANAGERS////////////////////////////////


        //Listing des managers enregistrés
        private void list_managers(ListBox maListe)
        {
            maListe.Items.Clear();

            foreach (managers s in (from e in db_remote.managers select e))
            {
                maListe.Items.Add(s.login);

            }
        }


        //Affichage de la liste des managers
        private void display_managers(String managers)
        {
            foreach (managers s in (from e in db_remote.managers select e))
            {
                if (s.login == managers)
                {
                    manager_login_textbox.Text = s.login;
                    manager_password_textbox.Password = s.password;
                    manager_firstname_textbox.Text = s.firstname;
                    manager_surname_textbox.Text = s.surname;

                    //Récupération, découpage et affichage des campus enregistrés pour un manager
                    manager_registred_campus_listbox.Items.Clear();

                    string[] table_campus;
                    string registred_campus = s.campus;
                    table_campus = registred_campus.Split('|');

                    for (int i = 0; i < table_campus.Length; i++)
                    {
                        manager_registred_campus_listbox.Items.Add(table_campus[i]);
                    }

                }

            }
        }


        //Rafraichissement des éléments
        private void refresh_managers()
        {
            manager_login_textbox.Text = "";
            manager_password_textbox.Password = "";
            manager_firstname_textbox.Text = "";
            manager_surname_textbox.Text = "";
            list_managers(manager_listbox);
            manager_registred_campus_listbox.Items.Clear();

            chargement_liste_campus_manager();

        }


        //Ajout d'un manager
        private void add_managers()
        {
            managers manager1 = new managers();

            manager1.login = manager_login_textbox.Text.ToLower();
            manager1.password = manager_password_textbox.Password;
            manager1.firstname = manager_firstname_textbox.Text.ToLower();
            manager1.surname = manager_surname_textbox.Text.ToLower();

            manager1.notepad_enabled = 1;
            manager1.notepad_text = "Write some notes here";


            //Concaténation de la sélection de campus puis enregistrement
            string concatenated_campus = "";

            for (int i = 0; i < manager_campus_listbox.SelectedItems.Count; i++)
            {
                string selected_campus = manager_campus_listbox.SelectedItems[i].ToString();

                if (concatenated_campus == "")
                {
                    concatenated_campus = String.Concat(selected_campus);
                }
                else
                {
                    concatenated_campus = String.Concat(concatenated_campus, "|", selected_campus);
                }
            }

            manager1.campus = concatenated_campus;


            db_remote.managers.InsertOnSubmit(manager1);
            db_remote.SubmitChanges();

            //Rafraichissement des éléments
            refresh_managers();


        }


        //Mise à jour d'un manager
        private void update_managers()
        {
            if (manager_login_textbox.Text != "" && manager_password_textbox.Password != "" && manager_firstname_textbox.Text != "" && manager_surname_textbox.Text != "")
            {
                managers manager2 = (from e in db_remote.managers
                                     where e.login == (string)manager_listbox.SelectedValue
                                     select e).FirstOrDefault();

                manager2.login = manager_login_textbox.Text.ToLower();
                manager2.password = manager_password_textbox.Password;
                manager2.firstname = manager_firstname_textbox.Text.ToLower();
                manager2.surname = manager_surname_textbox.Text.ToLower();


                //Concaténation de la sélection de campus puis enregistrement
                string concatenated_campus = "";

                for (int i = 0; i < manager_campus_listbox.SelectedItems.Count; i++)
                {
                    string selected_campus = manager_campus_listbox.SelectedItems[i].ToString();

                    if (concatenated_campus == "")
                    {
                        concatenated_campus = String.Concat(selected_campus);
                    }
                    else
                    {
                        concatenated_campus = String.Concat(concatenated_campus, "|", selected_campus);
                    }
                }

                manager2.campus = concatenated_campus;


                db_remote.SubmitChanges();

                //Rafraichissement des éléments
                refresh_managers();
            }

        }


        //Suppression d'un manager
        private void delete_managers()
        {
            if (manager_login_textbox.Text != "")
            {
                managers manager3 = (from e in db_remote.managers
                                     where e.login == (string)manager_listbox.SelectedValue
                                     select e).FirstOrDefault();

                db_remote.managers.DeleteOnSubmit(manager3);
                db_remote.SubmitChanges();

                //Rafraichissement des éléments
                refresh_managers();
            }

        }

        private void chargement_liste_campus_manager()
        {
            manager_campus_listbox.Items.Clear();

            foreach (campus s in (from e in db_remote.campus select e))
            {
                manager_campus_listbox.Items.Add(s.name);
            }
        }

        #endregion
    }
}
